package com.sgcc.cy.controller;

import com.sgcc.cy.dto.EmpEntity;
import com.sgcc.cy.file.FileUtils;
import com.sgcc.cy.poi.ExcelUtil;
import com.sgcc.cy.service.EmpService;
import com.sgcc.cy.util.JsonResult;
import com.sgcc.cy.util.redis.RedisUtil;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmpController {

    private static final Logger logger = LoggerFactory.getLogger(EmpController.class);

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private EmpService empService;

    @RequestMapping("/insert")
    public JsonResult<?> insert(EmpEntity empEntity){
        JsonResult<EmpEntity> jsonResult = JsonResult.newInstance();
        int insert = empService.insert(empEntity);
        if(insert == 1){
            //添加redis缓存
            redisUtil.set("123",empEntity);
            jsonResult.setCode(JsonResult.SUCCESS);
            jsonResult.setMsg("数据添加成功！");
        }else{
            jsonResult.setCode(JsonResult.FAILURE);
            jsonResult.setMsg("数据添加失败！");
        }
        return jsonResult;
    }

    @RequestMapping("/deleteById")
    public JsonResult<?>  deleteById(int id){
        JsonResult<EmpEntity> jsonResult = JsonResult.newInstance();
        int deleteById = empService.deleteById(id);
        if(deleteById >= 1){
            jsonResult.setCode(JsonResult.SUCCESS);
            jsonResult.setMsg("数据删除成功！");
        }else{
            jsonResult.setCode(JsonResult.FAILURE);
            jsonResult.setMsg("数据删除失败！");
        }
        return jsonResult;
    }

    @RequestMapping("/updateById")
    public JsonResult<?>  updateById(EmpEntity empEntity){
        JsonResult<EmpEntity> jsonResult = JsonResult.newInstance();
        int updateById = empService.updateById(empEntity);
        if(updateById >= 1){
            jsonResult.setCode(JsonResult.SUCCESS);
            jsonResult.setMsg("数据更新成功！");
        }else{
            jsonResult.setCode(JsonResult.FAILURE);
            jsonResult.setMsg("数据更新失败！");
        }
        return jsonResult;
    }

    @RequestMapping("/select")
    public JsonResult<List<EmpEntity>> select(){
        JsonResult<List<EmpEntity>> jsonResult = JsonResult.newInstance();
        List<EmpEntity> empEntityList = empService.select();
        jsonResult.setCode(JsonResult.SUCCESS);
        jsonResult.setMsg("查询数据成功！");
        jsonResult.setData(empEntityList);
        String a = redisUtil.get("123").toString();//测试Redis缓存
        return jsonResult;
    }

    @RequestMapping("/selectById")
    public JsonResult<EmpEntity> selectById(int id){
        JsonResult<EmpEntity> jsonResult = JsonResult.newInstance();
        EmpEntity empEntity = empService.selectById(id);
        jsonResult.setCode(JsonResult.SUCCESS);
        jsonResult.setMsg("查询数据成功！");
        jsonResult.setData(empEntity);
        return jsonResult;
    }
    @RequestMapping("/queryAll")
    public JsonResult<List<EmpEntity>> queryAll(){
        JsonResult<List<EmpEntity>> jsonResult = JsonResult.newInstance();
        List<EmpEntity> empEntitys = empService.queryAll();
        jsonResult.setCode(JsonResult.SUCCESS);
        jsonResult.setMsg("查询数据成功！");
        jsonResult.setData(empEntitys);
        return jsonResult;
    }



}
